package com.yitong.equipment.ui.mall

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.yitong.equipment.R
import com.yitong.equipment.interf.AppOnMultiListener
import com.yitong.equipment.base.BaseFragment
import com.yitong.equipment.databinding.FragmentMallBinding
import com.yitong.equipment.ext.init
import com.yitong.equipment.ui.adapter.*
import com.yitong.equipment.ui.viewmodel.MallViewModel
import com.yitong.equipment.utils.CommonUtil
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

class MallFragment : BaseFragment<MallViewModel, FragmentMallBinding>() {

    var imageUrls = listOf(
        "https://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png",
        "https://img.zcool.cn/community/016a2256fb63006ac7257948f83349.jpg",
        "https://img.zcool.cn/community/01233056fb62fe32f875a9447400e1.jpg",
        "https://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg"
    )

    private val mallMenuAdapter: HomeMenuAdapter by lazy { HomeMenuAdapter(arrayListOf()) }
    private val mallNewCourseAdapter: HomeNewCourseAdapter by lazy { HomeNewCourseAdapter(arrayListOf()) }
    private val mallHotCourseAdapter: HomeHotCourseAdapter by lazy { HomeHotCourseAdapter(arrayListOf()) }

    private var bannerHeight = 0

    override fun setContent(): Int {
        return R.layout.fragment_mall
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        ImmersionBar.setTitleBar(this, bindingView.homeTopLayout.toolbar)
//        bindingView.homeTopLayout.llScan.visibility = View.GONE
        val bannerParams: ViewGroup.LayoutParams = bindingView.banner.layoutParams
        val titleBarParams: ViewGroup.LayoutParams = bindingView.homeTopLayout.toolbar.layoutParams
        bannerHeight = bannerParams.height - titleBarParams.height - ImmersionBar.getStatusBarHeight(this)
        bindingView.nestedScrollView.run {
            setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
                var alpha = 0
                var scale = 0f
                override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                    if (scrollY <= bannerHeight) {
                        scale = scrollY.toFloat() / bannerHeight
                        alpha = (255 * scale).toInt()
                        bindingView!!.homeTopLayout.toolbar.setBackgroundColor(setColor(alpha))
                    } else {
                        if (alpha < 255) {
                            alpha = 255
                            bindingView!!.homeTopLayout.toolbar.setBackgroundColor(setColor(alpha))
                        }
                    }
                }
            })
        }

        bindingView.homeTopLayout.toolbar.run {
            setBackgroundColor(setColor(0))
        }
        bindingView.refreshLayout.run {
            setEnableLoadMore(false)
            setOnMultiListener(object : AppOnMultiListener {
                override fun onRefresh(refreshLayout: RefreshLayout) {
                    bindingView.refreshLayout.finishRefresh()
                }

                override fun onHeaderMoving(header: RefreshHeader?, isDragging: Boolean, percent: Float, offset: Int, headerHeight: Int, maxDragHeight: Int) {
                    if (percent == 0f) {
                        bindingView.homeTopLayout.toolbar.visibility = View.VISIBLE
                    } else {
                        bindingView.homeTopLayout.toolbar.visibility = View.GONE
                    }
                }
            })
        }

        bindingView.recycleView.init(GridLayoutManager(activity, 4), mallMenuAdapter, false)

        mallMenuAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                startActivity(Intent(activity, MallAllClassifyActivity::class.java))
            }
        }

        bindingView.recycleViewNewCourse.init(LinearLayoutManager(activity), mallNewCourseAdapter, false)
        bindingView.recycleViewHotCourse.init(GridLayoutManager(activity, 2), mallHotCourseAdapter, false)

        var linearLayoutManager = LinearLayoutManager(activity)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL

        var mutableListOf = mutableListOf<String>()
        for (index in 0..7) {
            mutableListOf.add("")
        }
        mallMenuAdapter.setList(CommonUtil.getHomeTopList())
        mallNewCourseAdapter.setList(mutableListOf)
        mallHotCourseAdapter.setList(mutableListOf)


        bindingView.banner.apply {
            addBannerLifecycleObserver(activity)
            setIndicator(CircleIndicator(activity))
            setAdapter(object : BannerImageAdapter<String>(imageUrls) {
                override fun onBindView(holder: BannerImageHolder, data: String, position: Int, size: Int) {
                    Glide.with(activity!!)
                        .load(data)
                        .into(holder.imageView)
                }
            })
        }

    }

    override fun initData() {
        createObserver()
    }

    private fun createObserver() {

    }

    override fun firstLoad() {
        viewModel.getData()
    }

    override fun notFirstLoad() {
        LogUtils.dTag("notFirstLoad", "notFirstLoad")
    }


    fun setColor(alpha: Int): Int {
        return Color.argb(alpha, 233, 90, 36)
    }

}